<?php

class ContentListElement
{
	
	protected $photo;
	/**
	 * Id of element.
	 * @var string
	 */
	protected $id;
	
	/**
	 * Name of element.
	 * @var string
	 */
	protected $name;

	/**
	 * Description of element.
	 * @var string
	 */
	protected $description;

	/**
	 * Target url.
	 * @var string
	 */
	protected $url;


	/**
	 * Rating for element
	 * @var string
	 */
	protected $rating;
	
	protected $text;


	/**
	 * Set base object for use in call.
	 *
	 * @param mixed $object
	 * @return ContentListElement
	 */
	public function __construct($object = null)
	{
		//$this->_object = $object;
	}

	/**
	 * Create new ContentListElement by any class implemented IContentListElementProvider.
	 * Also store base object inside.
	 *
	 * @param IContentListElementProvider $row
	 * @param string Abstract type of ContentListElement
	 * @return ContentListElement
	 */
	public static function fromRow($row, $type = null)
	{
		if (!$row instanceof IContentListElementProvider) {
			throw new Exception('Intefrace IContentListElementProvider not implemented');
		}

		$element = $row->getContentListElement($type);
		if($element){
			if($row instanceof ContentlistElementRow)
				$element->_object = $row->parent_row;
			else
				$element->_object = $row;
			return $element;
		} else return false;
	}

	public static function fromRowset($rowset, $params = array())
	{
		//var_dump($rowset);
		//$rowset = new Zend_Db_Table_Rowset();
		if (!$rowset instanceof ContentlistElementRowset) {
			$peer = call_user_func(array($rowset->getTableClass(), 'getInstance'));
			if ($peer instanceof Db_Peer) {
				$list = $peer->getContentList($rowset, $params);
			}
		} else {
			$list = $rowset->getContentList($params);
		}
		if ($list) {
			return $list;
		}
		return false;
	}
	
	/**
	 * Try to call function from parent object when ContentListElement have not this function.
	 *
	 * @param string $function
	 * @return mixed
	 */
	public function __call($function, $args)
	{
		//return call_user_func_array(array($this->_object, $function), $args);
		$debug = Debug::getInstance()->isDebugMode();
		
		// determite if it's 'get' or 'set'
		$action = substr($function, 0, 3);
		// replacing 'get' by empty string		
		$name = substr_replace($function, '', 0, 3);
		
		// Change camel case to variable name (Ex: FullDesc -> full_desc)
		$filter = new Zend_Filter_Word_CamelCaseToUnderscore();
		$varName = strtolower($filter->filter($name));
		
		// get list of class variables
		$vars = array_keys(get_object_vars($this));
		$class = get_class($this);
		if (!in_array($varName, $vars)) {
			if (!$debug) {
				return null;
			} else {
				throw new Exception("Variable $varName is not defined in class $class");	
			}
		}
		if ($action === 'get') {
			return $this->$varName;
		} elseif ($action === 'set') {
			$this->$varName = $args[0];
		} else {
			if ($debug) {
				throw new Exception("Wrong method name. There're only getters and setters aviable");
			}
		}
		
	}
	
	public function __get($name) {
		return $this->$name;
	}
	
	public function __set($name, $value){
		$this->$name = $value;
	}
	

}